<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="红娘等级" prop="matchmakerLevel">
        <el-input
          v-model="queryParams.matchmakerLevel"
          placeholder="请输入红娘等级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务区域" prop="serviceArea">
        <el-input
          v-model="queryParams.serviceArea"
          placeholder="请输入服务区域"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="从业年限" prop="serviceYears">
        <el-input
          v-model="queryParams.serviceYears"
          placeholder="请输入从业年限"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="100px" label="成功牵线次数" prop="successCount">
        <el-input
          v-model="queryParams.successCount"
          placeholder="请输入成功牵线次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <br>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['matchmaker:matchmaker:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['matchmaker:matchmaker:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['matchmaker:matchmaker:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['matchmaker:matchmaker:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="matchmakerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <el-table-column label="身份证号" align="center" prop="idCardNo" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="红娘等级：1-预备红娘，2-正式红娘，3-金牌红娘" align="center" prop="matchmakerLevel" />
      <el-table-column label="服务区域" align="center" prop="serviceArea" />
      <el-table-column label="从业年限" align="center" prop="serviceYears" />
      <el-table-column label="成功牵线次数" align="center" prop="successCount" />
      <el-table-column label="个人介绍" align="center" prop="introduction" />
      <el-table-column label="资质证书URL" align="center" prop="certification" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.certification" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="红娘状态：0-冻结，1-正常" align="center" prop="matchmakerStatus" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['matchmaker:matchmaker:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['matchmaker:matchmaker:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改红娘信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="matchmakerRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCardNo">
          <el-input v-model="form.idCardNo" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="红娘等级：1-预备红娘，2-正式红娘，3-金牌红娘" prop="matchmakerLevel">
          <el-input v-model="form.matchmakerLevel" placeholder="请输入红娘等级：1-预备红娘，2-正式红娘，3-金牌红娘" />
        </el-form-item>
        <el-form-item label="服务区域" prop="serviceArea">
          <el-input v-model="form.serviceArea" placeholder="请输入服务区域" />
        </el-form-item>
        <el-form-item label="从业年限" prop="serviceYears">
          <el-input v-model="form.serviceYears" placeholder="请输入从业年限" />
        </el-form-item>
        <el-form-item label="成功牵线次数" prop="successCount">
          <el-input v-model="form.successCount" placeholder="请输入成功牵线次数" />
        </el-form-item>
        <el-form-item label="个人介绍" prop="introduction">
          <el-input v-model="form.introduction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="资质证书URL" prop="certification">
          <image-upload v-model="form.certification"/>
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Matchmaker">
import { listMatchmaker, getMatchmaker, delMatchmaker, addMatchmaker, updateMatchmaker } from "@/api/matchmaker/matchmaker"

const { proxy } = getCurrentInstance()

const matchmakerList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    realName: null,
    idCardNo: null,
    phone: null,
    matchmakerLevel: null,
    serviceArea: null,
    serviceYears: null,
    successCount: null,
    introduction: null,
    certification: null,
    matchmakerStatus: null,
    createdAt: null,
    updatedAt: null
  },
  rules: {
    userId: [
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    realName: [
      { required: true, message: "真实姓名不能为空", trigger: "blur" }
    ],
    idCardNo: [
      { required: true, message: "身份证号不能为空", trigger: "blur" }
    ],
    phone: [
      { required: true, message: "联系电话不能为空", trigger: "blur" }
    ],
    matchmakerLevel: [
      { required: true, message: "红娘等级：1-预备红娘，2-正式红娘，3-金牌红娘不能为空", trigger: "blur" }
    ],
    successCount: [
      { required: true, message: "成功牵线次数不能为空", trigger: "blur" }
    ],
    matchmakerStatus: [
      { required: true, message: "红娘状态：0-冻结，1-正常不能为空", trigger: "change" }
    ],
    createdAt: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    updatedAt: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询红娘信息列表 */
function getList() {
  loading.value = true
  listMatchmaker(queryParams.value).then(response => {
    matchmakerList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    matchmakerId: null,
    userId: null,
    realName: null,
    idCardNo: null,
    phone: null,
    matchmakerLevel: null,
    serviceArea: null,
    serviceYears: null,
    successCount: null,
    introduction: null,
    certification: null,
    matchmakerStatus: null,
    createdAt: null,
    updatedAt: null
  }
  proxy.resetForm("matchmakerRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.matchmakerId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加红娘信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _matchmakerId = row.matchmakerId || ids.value
  getMatchmaker(_matchmakerId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改红娘信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["matchmakerRef"].validate(valid => {
    if (valid) {
      if (form.value.matchmakerId != null) {
        updateMatchmaker(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addMatchmaker(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _matchmakerIds = row.matchmakerId || ids.value
  proxy.$modal.confirm('是否确认删除红娘信息编号为"' + _matchmakerIds + '"的数据项？').then(function() {
    return delMatchmaker(_matchmakerIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('matchmaker/matchmaker/export', {
    ...queryParams.value
  }, `matchmaker_${new Date().getTime()}.xlsx`)
}

getList()
</script>